<?php
/**
 *+------------------
 * madong
 *+------------------
 * Copyright (c) https://gitee.com/motion-code  All rights reserved.
 *+------------------
 * Author: Mr. April (405784684@qq.com)
 *+------------------
 * Official Website: http://www.madong.tech
 */

namespace app\admin\controller\system;

use app\admin\controller\Crud;
use app\admin\validate\system\SysMenuValidate;
use app\common\services\system\SysMenuService;
use core\exception\handler\AdminException;
use core\utils\Json;
use support\Container;
use support\Request;

class SysMenuController extends Crud
{

    public function __construct()
    {
        parent::__construct();
        $this->service  = Container::make(SysMenuService::class);
        $this->validate = Container::make(SysMenuValidate::class);
    }

    public function store(Request $request): \support\Response
    {
        $this->service->cacheDriver()->delete(SysMenuService::CACHE_ALL_AUTHS_DATA);
        $this->service->cacheDriver()->delete(SysMenuService::CACHE_ALL_MENUS_DATA);
        return parent::store($request); // TODO: Change the autogenerated stub
    }

    public function update(Request $request): \support\Response
    {
        $this->service->cacheDriver()->delete(SysMenuService::CACHE_ALL_AUTHS_DATA);
        $this->service->cacheDriver()->delete(SysMenuService::CACHE_ALL_MENUS_DATA);
        return parent::update($request); // TODO: Change the autogenerated stub
    }

    /**
     * 删除
     *
     * @param \support\Request $request
     *
     * @return \support\Response
     */
    public function destroy(Request $request): \support\Response
    {
        try {
            $id   = $request->route->param('id');
            $data = $request->input('data', []);
            $data = !empty($id) && $id !== '0' ? $id : $data;
            if (empty($data)) {
                throw new AdminException('参数错误');
            }
            $result = $this->service->transaction(function () use ($data) {
                $data       = is_array($data) ? $data : explode(',', $data);
                $deletedIds = [];
                foreach ($data as $id) {
                    $item = $this->service->get($id);
                    if (!$item) {
                        continue; // 如果找不到项，跳过
                    }
                    $ids        = $item->deleteWithAllChildren();
                    $deletedIds = array_merge($deletedIds, $ids);
                }
                return array_unique($deletedIds);
            });
            $this->service->cacheDriver()->delete(SysMenuService::CACHE_ALL_AUTHS_DATA);
            $this->service->cacheDriver()->delete(SysMenuService::CACHE_ALL_MENUS_DATA);
            return Json::success('ok', $result);
        } catch (\Throwable $e) {
            return Json::fail($e->getMessage());
        }
    }

    /**
     * 批量添加菜单
     *
     * @param \support\Request $request
     *
     * @return \support\Response
     */
    public function batchStore(Request $request): \support\Response
    {
        try {
            $params = $request->input('menus', []);
            $data   = [];
            if (isset($this->validate) && $this->validate) {
                foreach ($params as $param) {
                    $data[] = $this->inputFilter($param);
                    if (!$this->validate->scene('batch-store')->check($param)) {
                        throw new \Exception($this->validate->getError());
                    }
                }
            }
            foreach ($data as $item) {
                $this->service->save($item);
            }
            $this->service->cacheDriver()->delete(SysMenuService::CACHE_ALL_AUTHS_DATA);
            $this->service->cacheDriver()->delete(SysMenuService::CACHE_ALL_MENUS_DATA);
            return Json::success('ok');
        } catch (\Exception $e) {
            return Json::fail($e->getMessage());
        }
    }

}
